﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TVShow.Framework
{
	public partial class MenuControl : UserControl, ILockRequester
	{
		static List<string> MenuPath = new List<string>();
		string GetLast() {
			if (MenuPath.Count > 0)
				return MenuPath[MenuPath.Count - 1];
			else
				return "Main Menu";
		}
		string GetPrev() {
			if (MenuPath.Count > 1)
				return MenuPath[MenuPath.Count - 2];
			else
				return "Main Menu";
		}
		void DropLast() {
			if (MenuPath.Count > 0)
				MenuPath.RemoveAt(MenuPath.Count - 1);
		}

		public MenuControl() {
			InitializeComponent();
		}

		private void MenuControl_Load(object sender, EventArgs e) {
		}

		int _selectedIndex;
		public int SelectedIndex {
			get { return _selectedIndex; }
			set {
				if (value < 0)
					_selectedIndex = 0;
				else if (value >= Items.Count)
					_selectedIndex = Items.Count - 1;
				else
					_selectedIndex = value;
				Items[_selectedIndex].Focus();
			}
		}

		string _MenuName;
		public string MenuName {
			get { return _MenuName; }
			set {
				_MenuName = value;
				if (MenuName == "Main Menu")
					MenuPath.Clear();
			}
		}

		DisplayManager Display;
		public List<MenuItem> Items = new List<MenuItem>();
		int ItemHeight = 20;
		public void ShowMenu(DisplayManager Display) {
			this.Display = Display;
			if (Display.MenuPanel.Lock(this)) {
				if (GetLast() != MenuName)
					MenuPath.Add(MenuName);

				Display.Header.Text = _MenuName;
				Panel Parent = flowLayoutPanel1;
				Display.MenuPanel.Add(this, this);

				this.Dock = DockStyle.Fill;
				ItemHeight = ClientRectangle.Height / 10;

				flowLayoutPanel1.Controls.Clear();
				for (int i = 0; i < Items.Count; i++) {
					MenuItem item = Items[i];
					item.Left = 0;
					item.Width = Parent.ClientRectangle.Width - 25;
					//item.Top = ItemHeight * i;
					item.Height = ItemHeight;
					item.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
					item.Click += new EventHandler(MenuItem_Click);
					item.Enter += new EventHandler(item_Enter);
					item.Index = i;
					Parent.Controls.Add(item);
					//item.BringToFront();
				}
				Parent.AutoScroll = true;
				Parent.AutoScrollMinSize = new Size(10, ItemHeight * Items.Count);
				SelectedIndex = 0;
			}
		}

		void item_Enter(object sender, EventArgs e) {
			MenuItem item = (MenuItem)sender;
			_selectedIndex = item.Index;
		}

		public void Add(string Text, string MessageData) {
			MenuItem item = new MenuItem();
			item.Text = Text;
			item.MessageData = MessageData;
			Items.Add(item);
		}

		void MenuItem_Click(object sender, EventArgs e) {
			MenuItem item = (MenuItem)sender;
			TVSMessage.Send(item.MessageData);
		}

		public void ProcessMessage(TVSMessage message) {
      if (message.Text == "action")
        ProcessAction(message[1]);
		}

    private void ProcessAction(string Action) {
      switch (Action.ToLower()) {
        case "escape":
        case "back":
          GoBack();
          break;
        case "pageup":
          SelectedIndex = Math.Max(0, SelectedIndex - 10);
          break;
        case "pagedown":
        case "next":
          SelectedIndex = Math.Min(Items.Count - 1, SelectedIndex + 10);
          break;
        case "up":
          SelectedIndex--;
          break;
        case "down":
          SelectedIndex++;
          break;
        case "select":
        case "enter":
          if (Items.Count > 0)
            MenuItem_Click(Items[SelectedIndex], new EventArgs());
          break;
      }
    }

		private void GoBack() {
			string Prev;
			if (MenuPath.Count > 0) {
				Prev = GetPrev();
				DropLast();
			}
			else
				Prev = "Main Menu";
			TVSMessage.Send("showmenu", Prev);
		}

		public void Clear() {
			this.Controls.Clear();
			Display.MenuPanel.Unlock(this);
		}

		#region ILockRequester Members

		public bool UnlockRequest(DisplayItem sender) {
			Items.Clear();
			Controls.Clear();
			Display.MenuPanel.Clear(this);
			return true;
		}

		#endregion
	}
}
